﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SManage.Model;


namespace SManage
{   
    public partial class frmShowProviderDetail : Form
    {string Conn = "server=127.0.0.1;database=wlaq;User ID=sa;pwd=sa;";//临时设置，需统一管理
        private string _sID;
        private string _sProvider;
        private string _sInDate;
        public string id {
            get { return _sID; }
            set { this._sID = value; }
        }
        public string ProviderName {
            get { return _sProvider; }
            set { this._sProvider = value; }
        }
        public string Indate { 
            get { return _sInDate;}
            set {this._sInDate= value;}
        }
        public frmShowProviderDetail()
        {
            InitializeComponent();
        }

        private void frmShowProviderDetail_Load(object sender, EventArgs e)
        {
            tb_DPID.Text = this.id;
            tb_DProviderName.Text = this.ProviderName;
            tb_DPInDate.Text = this.Indate;
            ShowTrueDetail();
        }
        private void ShowTrueDetail()
        {
            DataDeviceDataContext Devices = new DataDeviceDataContext(Conn);
            var result = from dConfig in Devices.T_DeviceConfiguration 
                         where dConfig.iID==Convert.ToInt32(this.id.ToString().Trim())
                         select new
                         {
                             设备编号 = dConfig.iDeviceId,
                            
                             设备种类 = dConfig.sType.ToString().Trim(),
                             品牌 = dConfig.sBrand.ToString().Trim(),
                             型号 = dConfig.sModel.ToString().Trim(),
                             CPU信息 = dConfig.sCPU.ToString().Trim(),
                             内存信息 = dConfig.sMemory.ToString().Trim(),
                             主板信息 = dConfig.sMainBoard.ToString().Trim() ,
                             硬盘信息 = dConfig.sHarddisk.ToString().Trim(),
                             采购编号=dConfig.iID,
                             备注 = dConfig.sRemark.ToString().Trim()

                         };
            dgv_DPDetail.DataSource = result;
 
        }

        private void tabControl1_Selected(object sender, TabControlEventArgs e)
        {
            if (e.TabPageIndex == 0)
            { ShowTrueDetail(); }
            else
            { ShowOneDetail(); }
        }

        private void ShowOneDetail()
        {
            DataDeviceDataContext Devices = new DataDeviceDataContext(Conn);
            var result = from dConfig in Devices.T_DeviceConfiguration
                         where dConfig.iID == Convert.ToInt32(this.id.ToString().Trim()) || dConfig.iID==0 orderby dConfig.iID ascending 
                         select new
                         {
                             设备编号 = dConfig.iDeviceId,

                             设备种类 = dConfig.sType.ToString().Trim(),
                             品牌 = dConfig.sBrand.ToString().Trim(),
                             型号 = dConfig.sModel.ToString().Trim(),
                             CPU信息 = dConfig.sCPU.ToString().Trim(),
                             内存信息 = dConfig.sMemory.ToString().Trim(),
                             主板信息 = dConfig.sMainBoard.ToString().Trim(),
                             硬盘信息 = dConfig.sHarddisk.ToString().Trim(),
                             采购编号 = dConfig.iID,
                             备注 = dConfig.sRemark.ToString().Trim()

                         };
            dgv_SetDetail.DataSource = result;

        }

        private void dgv_SetDetail_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgv_SetDetail.SelectedRows.Count != 0)
            {
                if (e.RowIndex >= 0)
                {
                    try
                    {
                        DataDeviceDataContext Devices = new DataDeviceDataContext(Conn);
                        var result = from dConfig in Devices.T_DeviceConfiguration
                                     where dConfig.iDeviceId == Convert.ToInt32(dgv_SetDetail.Rows[e.RowIndex].Cells[0].Value.ToString().Trim())
                                     select dConfig;
                        foreach (T_DeviceConfiguration dc in result)
                        {
                            if (dgv_SetDetail.Rows[e.RowIndex].Cells[8].Value.ToString().Trim() == "0")
                            { dc.iID = Convert.ToInt32(this.id.ToString().Trim()); }
                            else
                            { dc.iID = 0; }
                            
                            Devices.SubmitChanges();
                        }
                        MessageBox.Show("设置成功！");
                        ShowOneDetail();
                    }
                    catch
                    { MessageBox.Show("设置失败！"); }
                }
            }
        }
    }
}
